﻿namespace Vin.Extension.Cache;

public static class CacheFactory
{
    private static ICache? _cache = null;

    private static ICache Cache => _cache ?? new MemoryCache();

    /// <summary>
    /// 改变缓存对象
    /// </summary>
    /// <param name="cache"></param>
    public static void ChangeCacheObj(ICache cache)
    {
        _cache = cache;
    }

    public static bool SetCache<T>(string key, T value, TimeSpan? expireTime = null)
    {
        return Cache.SetCache(key, value, expireTime);
    }
    
    public static async Task<bool> SetCacheAsync<T>(string key, T value, TimeSpan? expireTime = null)
    {
        return await Cache.SetCacheAsync(key, value, expireTime);
    }

    public static T? GetCache<T>(string key)
    {
        return Cache.GetCache<T>(key);
    }
    
    public static async Task<T?> GetCacheAsync<T>(string key)
    {
        return await Cache.GetCacheAsync<T>(key);
    }

    public static bool RemoveCache(string key)
    {
        return Cache.RemoveCache(key);
    }
    
    public static async Task<bool> RemoveCacheAsync(string key)
    {
        return await Cache.RemoveCacheAsync(key);
    }
    
    public static bool SetHashCache<T>(string key, string field, T value, TimeSpan? expireTime = null)
    {
        return Cache.SetHashCache(key, field, value, expireTime);
    }
    
    public static async Task<bool> SetHashCacheAsync<T>(string key, string field, T value, TimeSpan? expireTime = null)
    {
        return await Cache.SetHashCacheAsync(key, field, value, expireTime);
    }
    
    public static T? GetHashCache<T>(string key, string field)
    {
        return Cache.GetHashCache<T>(key, field);
    }
    
    public static async Task<T?> GetHashCacheAsync<T>(string key, string field)
    {
        return await Cache.GetHashCacheAsync<T>(key, field);
    }
    
    public static bool RemoveHashCache(string key, string field)
    {
        return Cache.RemoveHashCache(key, field);
    }
    
    public static async Task<bool> RemoveHashCacheAsync(string key, string field)
    {
        return await Cache.RemoveHashCacheAsync(key, field);
    }
    
    public static long GetHashCount(string key)
    {
        return Cache.GetHashCount(key);
    }
    
    public static async Task<long> GetHashCountAsync(string key)
    {
        return await Cache.GetHashCountAsync(key);
    }

    public static long Incr(string key, TimeSpan? expireTime = null, bool firstSetExpire = true)
    {
        return Cache.Incr(key, expireTime, firstSetExpire);
    }
    
    public static async Task<long> IncrAsync(string key, TimeSpan? expireTime = null, bool firstSetExpire = true)
    {
        return await Cache.IncrAsync(key, expireTime, firstSetExpire);
    }

}